#ifndef __SHARE_H__
#define __SHARE_H__

template<class C>
class Autorelease{
public:
	Autorelease(C *obj):m_obj(obj){
	}
	~Autorelease(){
		if(m_obj){
			delete m_obj;
			m_obj = 0;
		}
	}
	C *operator->(){
		return m_obj;
	}
	C& operator*(){
		return m_obj;
	}
private:
	C *m_obj;
};

template<typename C>
class DataBuffer{
public:
	C *getBuffer(int len){
		int capa = m_size;
		while(capa < len){
			capa = capa * 3 / 2;
		}
		alloc(capa);
		return m_data;
	}
public:
	DataBuffer(int capacity = 64):m_data(0){
		if(capacity<1) capacity=64;
		alloc(capacity);
	}
	~DataBuffer(){
		if(m_data){
			delete m_data;
			m_data = 0;
		}
		m_size = 0;
	}
private:
	void alloc(int capacity){
		if(m_data){
			delete []m_data;
			m_data = 0;
		}
		m_data = new C[capacity];
		m_size = capacity;
	}
private:
	C *m_data;
	int m_size;
};

#endif